package com.agu.mgt.spmgt.controller;

import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.agu.common.annotation.SysLog;
import com.agu.common.excel.Column;
import com.agu.common.excel.ExcelExport;

import com.agu.utils.xss.XssHttpServletRequestWrapper;
import com.alibaba.fastjson.JSON;
import com.agu.mgt.sys.controller.AbstractController;
import com.agu.entity.spmgt.SpUserEntity;
import com.agu.mgt.spmgt.service.SpUserService;
import com.agu.utils.Query;
import com.github.pagehelper.Page;
import com.agu.result.Result;
import com.agu.result.ResultGenerator;
import com.agu.utils.CompareUtils;
import com.agu.utils.FormatUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * SP用户表
 * 
 * @author 
 * @email 
 * @date 2019-01-16 13:51:13
 */
 @SuppressWarnings("rawtypes")
@RestController
@Api("SP用户表")
@RequestMapping("spuser")
public class SpUserController extends AbstractController{
	@Autowired
	private SpUserService spUserService;
	
	/**
	 * 列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/list")
	@ApiOperation(value = "列表", notes = "说明")
	@RequiresPermissions("entsp:list")
	public Result list(@RequestBody Map params){

		// 查询列表数据
		
		List<SpUserEntity>  list= spUserService.queryList(params);
		
		Page<SpUserEntity> page = new Page();
		
		page.addAll(list);
		
		return ResultGenerator.successPage(page);
	}
	
	
	/**
	 * 信息
	 */
	@GetMapping("/info/{userId}")
	@ApiOperation(value = "信息", notes = "说明")
	@RequiresPermissions("entsp:list")
	public Result info(@PathVariable("userId") Long userId){
		SpUserEntity spUser = spUserService.queryObject(userId);
		
		return ResultGenerator.successData(spUser);
	}
	
	/**
	 * 保存
	 */
	@SysLog("新增SP用户表")
	@PostMapping("/save")
	@ApiOperation(value = "保存", notes = "说明")
	@RequiresPermissions("entsp:list")
	public Result save(@RequestBody SpUserEntity spUser){
	
		
		spUser.setCreateUserId(getUserId());
		spUser.setCreateTime(new Date());
		spUserService.save(spUser);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		     cmap.put("userId","ID");
		     cmap.put("userName","用户名");
		     cmap.put("password","密码");
		     cmap.put("email","邮箱");
		     cmap.put("mobile","手机号");
		     cmap.put("userType","用户类别");
		     cmap.put("name","姓名");
		     cmap.put("status","状态  ");
		     cmap.put("plat","平台");
		     cmap.put("createUserId","创建者ID");
		     cmap.put("createTime","创建时间");
		String remark = CompareUtils.getObjectRemark(spUser, cmap);
		return ResultGenerator.success("保存成功").setRemark(remark);
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改SP用户表")
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "说明")
	@RequiresPermissions("entsp:list")
	public Result update(@RequestBody SpUserEntity spUser){
	
		SpUserEntity spUser1 = spUserService.queryObject(spUser.getUserId());
	
		
		if(StringUtils.isNotBlank(spUser.getPassword()))
		{
			//sha256加密
			String newPassword = new Sha256Hash(spUser.getPassword()).toHex();
			
			spUser.setPassword(newPassword);
		}
		
		spUserService.update(spUser);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		
		     cmap.put("userId","ID");
		     cmap.put("userName","用户名");
		     //cmap.put("password","密码");
		     cmap.put("email","邮箱");
		     cmap.put("mobile","手机号");
		     cmap.put("userType","用户类别");
		     cmap.put("name","姓名");
		     cmap.put("status","状态  ");
		     cmap.put("plat","平台");
		     cmap.put("createUserId","创建者ID");
		     cmap.put("createTime","创建时间");
	
		String remark = CompareUtils.getTwoObjectRemark(spUser1, spUser, cmap);
	
		return ResultGenerator.success("修改成功").setRemark(remark);
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除SP用户表")
	@GetMapping("/delete/{userId}")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("entsp:list")
	public Result delete(@PathVariable("userId") Long userId){
	
	    	if (userId == null) {
			return ResultGenerator.fail("缺少参数");
		}
	
		SpUserEntity spUser = spUserService.queryObject(userId);
	
		spUserService.delete(userId);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		     cmap.put("userId","ID");
		     cmap.put("userName","用户名");
		     cmap.put("password","密码");
		     cmap.put("email","邮箱");
		     cmap.put("mobile","手机号");
		     cmap.put("userType","用户类别");
		     cmap.put("name","姓名");
		     cmap.put("status","状态  ");
		     cmap.put("plat","平台");
		     cmap.put("createUserId","创建者ID");
		     cmap.put("createTime","创建时间");
		String remark = CompareUtils.getObjectRemark(spUser, cmap);
		return ResultGenerator.success("删除成功").setRemark(remark);
	}
	
	/**
	 * 批量删除
	 */
	@SysLog("批量删除SP用户表")
	@PostMapping("/deletes")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("entsp:list")
	public Result deletes(@RequestBody Long[] userIds){
	
		spUserService.deleteBatch(userIds);
		
		return ResultGenerator.success("删除成功");
	}
	
	@RequestMapping("/download")
	@ApiOperation(value = "下载", notes = "说明")
	@RequiresPermissions("spuser:list")
	public void download(HttpServletRequest request, HttpServletResponse response) {

		HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);
		
		String query = orgRequest.getParameter("query");

		HashMap querymap = JSON.parseObject(query, HashMap.class);

		List<SpUserEntity> list = spUserService.queryList(querymap);

		if (list != null && list.size() > 0) {
			Map map = new HashMap();

               Map<Integer, String> userTypeMap = new HashMap<Integer, String>();

                userTypeMap.put(1, "管理员");
                userTypeMap.put(2, "操作用户");
            map.put("userTypeMap", userTypeMap);
               Map<Integer, String> statusMap = new HashMap<Integer, String>();

                statusMap.put(0, "禁用");
                statusMap.put(1, "正常");
            map.put("statusMap", statusMap);

			List<Column> clist = new ArrayList();
			Column cloumn = null;

            cloumn = new Column("ID", "userId", 1);
			clist.add(cloumn);
            cloumn = new Column("用户名", "userName", 1);
			clist.add(cloumn);
            cloumn = new Column("密码", "password", 1);
			clist.add(cloumn);
            cloumn = new Column("邮箱", "email", 1);
			clist.add(cloumn);
            cloumn = new Column("手机号", "mobile", 1);
			clist.add(cloumn);
            cloumn = new Column("用户类别", "userType", 2);
			clist.add(cloumn);
            cloumn = new Column("姓名", "name", 1);
			clist.add(cloumn);
            cloumn = new Column("状态  ", "status", 2);
			clist.add(cloumn);
            cloumn = new Column("平台", "plat", 1);
			clist.add(cloumn);
            cloumn = new Column("创建者ID", "createUserId", 1);
			clist.add(cloumn);
            cloumn = new Column("创建时间", "createTime", 1);
			clist.add(cloumn);
			try {
				ExcelExport export = exportList(list, clist, map);
				if (export != null) {
					this.downExcel(export, response);
					//this.downzip(export, response);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
